package com.subaiqiao.yunshiyuan.entrance.ysy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.subaiqiao.yunshiyuan.basis.exception.BusinessException;
import com.subaiqiao.yunshiyuan.basis.service.impl.BasisServiceImpl;
import com.subaiqiao.yunshiyuan.basis.utils.CreateJwt;
import com.subaiqiao.yunshiyuan.basis.utils.Encryption;
import com.subaiqiao.yunshiyuan.basis.utils.Result;
import com.subaiqiao.yunshiyuan.entrance.ysy.Facade.VerificationCodeFacade;
import com.subaiqiao.yunshiyuan.entrance.ysy.mapper.UserInfoMapper;
import com.subaiqiao.yunshiyuan.entrance.ysy.pojo.UserInfo;
import com.subaiqiao.yunshiyuan.entrance.ysy.service.UserInfoService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 曹朝禹
 * @since 2022-05-13
 */
@Service
@Transactional
public class UserInfoServiceImpl extends BasisServiceImpl<UserInfo, UserInfoMapper> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @SneakyThrows
    @Override
    public boolean save(UserInfo entity) {
        entity.setPassword(Encryption.encrypt2ToMD5(entity.getPassword()));
        entity.setStatus(0);
        entity.setUserType(1);
        return userInfoMapper.insert(entity) > 0;
    }

    @SneakyThrows
    @Override
    public boolean updateById(UserInfo entity) {
        if(entity.getPassword()!=null)
            entity.setPassword(Encryption.encrypt2ToMD5(entity.getPassword()));
        return userInfoMapper.updateById(entity) > 0;
    }

    @Override
    public Integer FindUserCount() {
        return baseMapper.FindUserCount();
    }

    @Override
    public Integer FindUserDisabledCount() {
        return baseMapper.FindUserDisabledCount();
    }

    @Override
    public Integer FindUserYesterdayRegister() {
        return baseMapper.FindUserYesterdayRegister();
    }

    @Override
    public void UpdateToken(String token,Integer Id) {
        baseMapper.UpdateToken(token, Id);
    }

    @Override
    public UserInfo RootLogin(String username, String password) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        try {
            wrapper.eq("username", username)
                    .eq("password", Encryption.encrypt2ToMD5(password))
                    .eq("user_type", 0);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        UserInfo user = getOne(wrapper);
        if (user != null && user.getUserType() == 0) {
            String token = CreateJwt.getoken(user.getUserId(), user.getUsername());
            user.setToken(token);
            user.setPassword(password);
            updateById(user);
            return getById(user.getUserId());
        } else {
            throw new BusinessException("用户名或密码错误");
        }
    }

    @Override
    public UserInfo Login(String username, String password) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        try {
            wrapper.eq("username",username).eq("password", Encryption.encrypt2ToMD5(password));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        UserInfo user = getOne(wrapper);
        if(user!=null){
            String token = CreateJwt.getoken(user.getUserId(), user.getUsername());
            user.setToken(token);
            user.setPassword(password);
            updateById(user);
            return getById(user.getUserId());
        }else{
            throw new BusinessException("用户名或密码错误");
        }
    }

    @Override
    public UserInfo ChangePasswordByOldPassword(String username, String oldPassword, String newPassword) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        try {
            wrapper.eq("username",username).eq("password", Encryption.encrypt2ToMD5(oldPassword));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        UserInfo user = getOne(wrapper);
        if(user!=null && user.getUserType()==1){
            user.setPassword(newPassword);
            updateById(user);
            return getById(user.getUserId());
        }else{
            throw new BusinessException("旧密码错误");
        }
    }

    @Override
    public UserInfo ChangePasswordByPhone(String username, String phone, String ViefCode, String newPassword) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        if(!username.equals("")){
            wrapper.eq("username",username);
        }
        if(!phone.equals("")){
            wrapper.eq("phone",phone);
        }
        UserInfo user = getOne(wrapper);
        if(user!=null){
            if(VerificationCodeFacade.VerificationCodeJudgment(user.getPhone(),"找回密码","0",ViefCode)){
                user.setPassword(newPassword);
                updateById(user);
                return user;
            }
            throw new BusinessException("验证码输入错误或已过期！");
        }else{
            throw new BusinessException("无该用户！");
        }
    }

    @Override
    public UserInfo ViefLogin(String Phone, String ViefCode) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", Phone);
        UserInfo user = getOne(wrapper);
        if(user!=null){
            if(VerificationCodeFacade.VerificationCodeJudgment(Phone,"登录","0",ViefCode)){
                String token= CreateJwt.getoken(user.getUserId(), user.getUsername());
                UpdateToken(token, user.getUserId());
                return getOne(wrapper);
            }
            throw new BusinessException("验证码输入错误或已过期！");
        }else{
            throw new BusinessException("无该用户！");
        }
    }

    @Override
    public Integer FindCount() {
        return null;
    }

    @Override
    public UserInfoMapper getMapper() {
        return userInfoMapper;
    }
}
